<template>
  <div class="xd-washing-role">
      <div class="list" v-show="isShow">
        <Card>
          <p slot="title">
            <Icon type="md-list" />
            洗涤账号列表
          </p>
          <Form ref="formInline" :model="formSearch"  inline>
            <FormItem prop="user">
              <Input type="text" v-model="formSearch.name" placeholder="请输入洗涤公司名称"></Input>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleSeacrh()">查询</Button>
            </FormItem>
            <FormItem>
              <Button type="primary" @click="handleAdd()">新增账号</Button>
            </FormItem>
          </Form>
          <Table highlight-row stripe :columns="columns" :data="data" ></Table>
          <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
            </div>
          </div>
          <Modal
            v-model="modal"
            footer-hide="true"
            transfer="false"
            >
            <p slot="header">
              <Icon type="ios-paper" />
              <span>查看详情</span>
            </p>
            <p class="viewLine"><span>序号:</span> {{currentData.id}}</p>
            <p class="viewLine"><span>用户标识:</span>{{currentData.userid}}</p>
            <p class="viewLine"><span>用户名:</span>{{currentData.username}}</p>
            <p class="viewLine"><span>单位名称:</span>{{currentData.name}}</p>
            <p class="viewLine"><span>角色:</span>{{currentData.rolename}}</p>
            <p class="viewLine"><span>类别:</span>{{currentData.category}}</p>
            <p class="viewLine"><span>电话:</span>{{currentData.tel}}</p>
            <p class="viewLine"><span>创建时间:</span>{{formatTime(currentData.creationDate)}}</p>
            <p class="viewLine"><span>更新时间:</span>{{formatTime(currentData.EditDate)}}</p>
            <p class="viewLine"><span>备注:</span>{{currentData.remark}}</p>
          </Modal>
        </Card>
      </div>
      <div class="add" v-show="!isShow">
        <Card>
          <p slot="title">
            <Icon type="ios-create" v-show="this.type!=='edit'"/>
            <Icon type="ios-document" v-show="this.type=='edit'"/>
            {{this.type == "edit" ? "洗涤账号编辑" : "洗涤账号新增"}}
          </p>
          <p slot="extra" >
            <Button type="primary" @click="goBack()">返回</Button>
          </p>
          <div class="editForm">
            <Form ref="editFormWashRole" :model="editFormInLine" :rules="editFormRuleInline" :label-width="80">
              <FormItem label="单位名称" prop="depaid">
                <Select class="select" v-model="editFormInLine.depaid">
                  <Option v-for="item in washList" :value="item.depaid" :key="item.depaid">
                  {{ item.depaname }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="角色" prop="role">
                <Select class="select" v-model="editFormInLine.role">
                  <Option v-for="item in roleList" :value="item.roleid" :key="item.roleid">
                  {{ item.rolename }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="用户名" prop="username">
                <Input v-model="editFormInLine.username" placeholder="请输入用户名"></Input>
              </FormItem>
              <FormItem label="手机"  prop="tel">
                <Input v-model="editFormInLine.tel" placeholder="请输入电话"></Input>
              </FormItem>
              <FormItem label="备注" prop="remark">
                <Input v-model="editFormInLine.remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注" maxlength="50"></Input>
              </FormItem>
              <FormItem>
                <Button @click="handleSubmit" type="primary" class="button">提交</Button>
                <Button @click="handleReset" type="primary" class="button">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Card>
      </div>
    </Card>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import { formatNetTime } from "@/libs/tools";
import i18n from "./locale";
import mixinTag from "@/mixin/tag-mixin";

export default {
  name: "xd-washing-role",
  components: {},
  mixins: [mixinTag],
  data() {
    return {
      i18n: i18n,
      modal: false,
      isShow: true,
      currentData: "",
      type: "",
      roleList: [],
      washList: [],
      page: {
        total: 0,
        pageSize: 20
      },
      editFormInLine: {
        depaid: "",
        category: "",
        role: "",
        username: "",
        tel: "",
        remark: ""
      },
      editFormRuleInline: {
        depaid: ValidatorRule.selectNum,
        category: ValidatorRule.selectNum,
        role: ValidatorRule.selectNum,
        username: ValidatorRule.userChineseName,
        tel: ValidatorRule.tel
      },
      formSearch: {
        name: "",
        type: "1",
        pageSize: 20,
        pageNum: 1
      },
      columns: [
        {
          title: "序号",
          key: "id"
        },
        {
          title: "用户标识",
          key: "userid"
        },
        {
          title: "用户名",
          key: "username"
        },
        {
          title: "公司名称",
          key: "name"
        },
        {
          title: "角色",
          key: "rolename"
        },
        {
          title: "类别",
          key: "category"
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.show(params.index);
                    }
                  }
                },
                "查看"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.index);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.delete(params.index, params.row.id);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: []
    };
  },

  mounted() {
    this.resetInit();
    this.getWashlList();
  },

  activated() {
    this.getWashlList();
  },

  methods: {
    resetInit() {
      this.handleSeacrh();
      this.roleList = sessionStorage.getItem("roleList")
        ? JSON.parse(sessionStorage.getItem("roleList"))
        : [];
    },

    formatTime: function(timeStr1) {
      return formatNetTime(timeStr1);
    },

    getWashlList() {
      API.getWashlList({ type: 1 })
        .then(res => {
          if (res.code == 200) {
            this.washList = res.data;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleSeacrh() {
      let obj = this.formSearch;
      obj.pageNum = 1;
      this.formSearch = obj;
      this.getList(obj);
    },

    changePage(pageNum) {
      let obj = this.formSearch,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = obj;
      this.getList(obj);
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = this.formSearch;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          if (res.code == 200) {
            let data = res.data;
            this.page.total = data.total;
            this.page.pageSize = data.pageSize;
            this.data = data.list;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleAdd() {
      this.isShow = false;
      this.type = "add";
      this.currentData = "";
      this.handleReset();
    },

    show(index) {
      this.currentData = this.data[index];
      this.modal = true;
    },

    edit(index) {
      this.isShow = false;
      this.type = "edit";
      this.currentData = this.data[index];
      //赋值
      this.editFormInLine.depaid = this.currentData.pdepaid;
      this.editFormInLine.category = this.currentData.category;
      this.editFormInLine.role = this.currentData.roleid;
      this.editFormInLine.username = this.currentData.username;
      this.editFormInLine.tel = this.currentData.tel;
      this.editFormInLine.remark = this.currentData.remark;
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            id: id,
            type: "user"
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              console.log(err);
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    goBack() {
      this.isShow = true;
      this.handleReset();
    },

    handleReset() {
      this.$refs.editFormWashRole.resetFields();
    },

    handleSubmit() {
      this.$refs.editFormWashRole.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    editSubmit(obj) {
      obj.id = this.currentData.id;
      API.editSubmit(obj)
        .then(res => {
          this.resetList();
          this.$Message.success("修改成功！");
          this.goBack();
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      obj.category = "洗涤公司";
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSeacrh();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./xd-washing-role.less";
</style>
